package com.ynunicom.dc.dingdingcontractapp.controller;

import com.alibaba.fastjson.JSONObject;
import com.ynunicom.dc.dingdingcontractapp.dto.ResponseDto;
import com.ynunicom.dc.dingdingcontractapp.entity.StandReturnerImpl;
import com.ynunicom.dc.dingdingcontractapp.entity.entityinterface.StandReturner;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.*;
import com.ynunicom.dc.dingdingcontractapp.service.UserOptionService;
import com.ynunicom.dc.dingdingcontractapp.service.impl.UserOptionServiceImpl;
import lombok.SneakyThrows;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author: jinye.Bai
 * @date: 2020/5/25 14:05
 */
@RestController
@RequestMapping("/user")
public class UserOptionController {

    @Resource
    UserOptionService userOptionService;

    @Resource
    StandReturner standReturner;


    private static Class<?> userOptionServiceClass = UserOptionServiceImpl.class;

    @SneakyThrows
    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping("/createNewStaff")
    public ResponseDto<JSONObject> createNewStaff(@Validated @RequestBody NewStaffRequestBody newStaffRequestBody, @RequestParam(value = "access_token",required = true) String access_token){
        TransBody<NewStaffRequestBody> transBody = new TransBody<>(newStaffRequestBody,access_token);
        return standReturner.doReturn(transBody,userOptionServiceClass,userOptionServiceClass.getMethod("createNewStaff",TransBody.class));
    }

    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @PutMapping("/updateStaff")
    public ResponseDto<JSONObject> updateStaff(@Validated @RequestBody UpdateStaffRequestBody updateStaffRequestBody,@RequestParam(value = "access_token",required = true) String access_token){
        TransBody<UpdateStaffRequestBody> transBody = new TransBody<>(updateStaffRequestBody,access_token);
        return standReturner.doReturn(transBody,userOptionServiceClass,userOptionServiceClass.getMethod("updateStaff",TransBody.class));
    }

    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @DeleteMapping("/del/{userId}")
    public ResponseDto<JSONObject> deleteStaff(@PathVariable("userId") String userId,@RequestParam(value = "access_token",required = true) String access_token){
        TransBody<String> transBody = new TransBody<>(userId,access_token);
        return standReturner.doReturn(transBody,userOptionServiceClass,userOptionServiceClass.getMethod("deleteStaff",TransBody.class));
    }

    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @GetMapping("/get/{userId}")
    public ResponseDto<JSONObject> getUserInfo(@PathVariable("userId") String userId,@RequestParam(value = "access_token",required = true) String access_token){
        TransBody<String> transBody = new TransBody<>(userId,access_token);
        return standReturner.doReturn(transBody,userOptionServiceClass,userOptionServiceClass.getMethod("getUserInfo",TransBody.class));
    }

    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @GetMapping("/allId/{deptId}")
    public ResponseDto<JSONObject> getDeptUserId(@PathVariable("deptId") String deptId,@RequestParam(value = "access_token",required = true) String access_token){
        TransBody<String> transBody = new TransBody<>(deptId,access_token);
        return standReturner.doReturn(transBody,userOptionServiceClass,userOptionServiceClass.getMethod("getDeptUserId",TransBody.class));
    }

    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @GetMapping("/allInfo/{department_id}")
    public ResponseDto<JSONObject> getDeptUserInfo(@PathVariable("department_id") String department_id,@RequestParam(value = "access_token",required = true) String access_token){
        TransBody<String> transBody = new TransBody<>(department_id,access_token);
        return standReturner.doReturn(transBody,userOptionServiceClass,userOptionServiceClass.getMethod("getDeptUserInfo",TransBody.class));
    }

    /**
     * 返回整个部门的用户细节
     * @param department_id
     * @param access_token
     * @param pageRequestEntity
     * @return
     */
    @ResponseStatus(HttpStatus.ACCEPTED)
    @PostMapping("/allDetail/{department_id}")
    public ResponseDto<JSONObject> getDeptUserDetail(@PathVariable("department_id") String department_id, @RequestParam(value = "access_token",required = true) String access_token, @RequestBody @Validated PageRequestEntity pageRequestEntity){

        TransBody<PageRequestEntity> transBody = new TransBody<>(pageRequestEntity,access_token);
        JSONObject returner = userOptionService.getDeptUserDetail(transBody,department_id);
        return Integer.parseInt(returner.getString("errcode"))!=0? ResponseDto.failed("请求失败",returner): ResponseDto.success(returner);
    }

    /**
     * 根据unionid获取userid
     */
    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @GetMapping("/unionId2UserId")
    public ResponseDto<JSONObject> exchangeUnionId2UserId(@RequestParam(value = "unionId",required = true) String unionId,@RequestParam(value = "access_token",required = true) String access_token){
        TransBody<String> transBody = new TransBody<>(unionId,access_token);
        return standReturner.doReturn(transBody,userOptionServiceClass,userOptionServiceClass.getMethod("exchangeUnionId2UserId",TransBody.class));
    }

    /**
     * 根据手机号获取userid
     */
    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @GetMapping("/mobile/{mobile}")
    public ResponseDto<JSONObject> mobileToUserId(@PathVariable(value = "mobile",required = true) String mobile,@RequestParam(value = "access_token",required = true) String access_token){
        TransBody<String> transBody = new TransBody<>(mobile,access_token);
        return standReturner.doReturn(transBody,userOptionServiceClass,userOptionServiceClass.getMethod("mobileToUserId",TransBody.class));
    }

    @SneakyThrows
    @ResponseStatus(HttpStatus.ACCEPTED)
    @GetMapping("/allUpperDeptPath")
    public ResponseDto<JSONObject> allUpperDeptPath(@RequestParam(value = "access_token") String access_token,@RequestParam("userId") String userId){
        TransBody<String> transBody = new TransBody<>(userId,access_token);
        return standReturner.doReturn(transBody,userOptionServiceClass,userOptionServiceClass.getMethod("allUpperDeptPath",TransBody.class));
    }
}
